package br.faimg.pomar.modelo.dao;

import br.faimg.pomar.modelo.interfaces.CRUDInterface;
import br.faimg.pomar.modelo.pojo.TipodeErro;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

public class TipodeErroDAO implements CRUDInterface<TipodeErro> {

    public void create(TipodeErro pojo) {

        Connection con = null;
        PreparedStatement stm = null;
        String sql = "insert into TIPO_ERRO values(?,?);";

        try {

            con = ConnectionManager.getInstance().getConnection();
            stm = con.prepareStatement(sql);
            stm.setString(1, pojo.getCodigo());
            stm.setString(2, pojo.getDescricao());
            stm.execute();

        } catch (SQLException ex) {
            Logger.getLogger(TipodeErroDAO.class.getName()).log(Level.SEVERE, null, ex);
        } finally {

            if (stm != null) {

                try {
                    stm.close();
                } catch (SQLException ex) {
                    Logger.getLogger(TipodeErroDAO.class.getName()).log(Level.SEVERE, null, ex);
                }

            }


            if (con != null) {

                try {
                    con.close();
                } catch (SQLException ex) {
                    Logger.getLogger(TipodeErroDAO.class.getName()).log(Level.SEVERE, null, ex);
                }

            }

        }
    }

    public TipodeErro readUltimoDefeito(String numerodeserie) {
        Connection con = null;
        PreparedStatement stm = null;
        ResultSet rs = null;
        String sql = "select * from producao,tipo_erro where pdc_trr_codigo = trr_codigo and pdc_nus_numero = ? order by pdc_data,pdc_hora desc limit 1";
        TipodeErro tipodeerro = null;

        if (numerodeserie != null && !numerodeserie.equals("")) {

            con = ConnectionManager.getInstance().getConnection();
            try {
                stm = con.prepareStatement(sql);
                stm.setString(1, numerodeserie);
                rs = stm.executeQuery();

                while (rs.next()) {
                    TipodeErro temp = new TipodeErro();
                    temp.setCodigo(rs.getString("trr_codigo"));
                    temp.setDescricao(rs.getString("trr_descricao"));
                    tipodeerro = temp;
                }

            } catch (SQLException ex) {
                Logger.getLogger(TipodeErroDAO.class.getName()).log(Level.SEVERE, null, ex);
            }

        }

        return tipodeerro;
    }

    public void update(TipodeErro pojo) {
        Connection con = null;
        PreparedStatement stm = null;
        String sql = "update TIPO_ERRO set trr_descricao = ? where trr_codigo  = ?;";

        try {

            con = ConnectionManager.getInstance().getConnection();
            stm = con.prepareStatement(sql);
            stm.setString(1, pojo.getDescricao());
            stm.setString(2, pojo.getCodigo());
            stm.execute();

        } catch (SQLException ex) {
            Logger.getLogger(TipodeErroDAO.class.getName()).log(Level.SEVERE, null, ex);
        } finally {

            if (stm != null) {

                try {
                    stm.close();
                } catch (SQLException ex) {
                    Logger.getLogger(TipodeErroDAO.class.getName()).log(Level.SEVERE, null, ex);
                }

            }


            if (con != null) {

                try {
                    con.close();
                } catch (SQLException ex) {
                    Logger.getLogger(TipodeErroDAO.class.getName()).log(Level.SEVERE, null, ex);
                }

            }

        }

    }

    public List<TipodeErro> readByExample(TipodeErro pojo) {
        Connection con = null;
        PreparedStatement stm = null;
        ResultSet rs = null;
        String sql = "select * from TIPO_ERRO where trr_descricao ilike ?";
        List<TipodeErro> tipodeerros = new ArrayList<TipodeErro>();

        if (pojo == null) {

            pojo = new TipodeErro();
        }

        if (pojo.getDescricao() == null) {

            pojo.setDescricao("");

        }

        con = ConnectionManager.getInstance().getConnection();
        try {
            stm = con.prepareStatement(sql);
            stm.setString(1, "%"+ pojo.getDescricao() +"%");
            rs = stm.executeQuery();

            while (rs.next()) {
                TipodeErro temp = new TipodeErro();
                temp.setCodigo(rs.getString("trr_codigo"));
                temp.setDescricao(rs.getString("trr_descricao"));
                tipodeerros.add(temp);
            }

        } catch (SQLException ex) {
            Logger.getLogger(TipodeErroDAO.class.getName()).log(Level.SEVERE, null, ex);
        }

        return tipodeerros;
    }

    public TipodeErro readById(Character codigo) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void delete(Character codigo) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public TipodeErro readById(String codigo) {
        Connection con = null;
        PreparedStatement stm = null;
        ResultSet rs = null;
        String sql = "select * from TIPO_ERRO where trr_codigo = ?";
        TipodeErro tipodeerro = null;

        if (codigo != null && !codigo.equals("")) {

            con = ConnectionManager.getInstance().getConnection();
            try {
                stm = con.prepareStatement(sql);
                stm.setString(1, codigo);
                rs = stm.executeQuery();

                while (rs.next()) {
                    TipodeErro temp = new TipodeErro();
                    temp.setCodigo(rs.getString("trr_codigo"));
                    temp.setDescricao(rs.getString("trr_descricao"));
                    tipodeerro = temp;
                }

            } catch (SQLException ex) {
                Logger.getLogger(TipodeErroDAO.class.getName()).log(Level.SEVERE, null, ex);
            }

        }
         
        return tipodeerro;
    }

    public void delete(String codigo) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public TipodeErro readById(Integer codigo) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void delete(Integer codigo) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

}
 
